export function loadJS(src) {
  return new Promise((resolve, reject) => {
    var script = document.createElement('script')
    script.type = 'text/javascript'
    script.async = false
    script.src = src

    script.onload = function () {
      resolve()
    }
    script.onerror = function () {
      reject()
    }
    document.getElementsByTagName('head')[0].appendChild(script)
  })
}

export function loadAsyncSDK(srcList = []) {
  return new Promise((resolve, reject) => {
    const asyncList = srcList.map((src) => loadJS(src))
    if (!asyncList.length) {
      resolve()
      return
    }

    Promise.all(asyncList)
      .then(() => resolve())
      .catch(() => reject())
  })
}
